import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from "../views/Home/Home";
import Login from '../views/Login/Login.vue'
import UserList from '../views/User/UserList'
import store from '../store'
import { Message } from "element-ui"

Vue.use(VueRouter);

const routes = [{
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/user-list',
        children: [{
                path: '/user-list',
                name: 'UserList',
                component: UserList,
                meta: {
                    // requireAuth: true
                }
            },
            {
                path: '/user-detail',
                name: 'UserDetail',
                component: () =>
                    import ('../views/User/UserDetail'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/shop-list',
                name: 'ShopList',
                component: () =>
                    import ('../views/Shop/ShopList'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/shop-detail',
                name: 'ShopDetail',
                component: () =>
                    import ('../views/Shop/ShopDetail'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/coach-list',
                name: 'CoachList',
                component: () =>
                    import ('../views/Coach/CoachList'),
                meta: {
                    requireAuth: true
                }
            }, {
                path: '/coachCourse',
                name: 'CoachCourse',
                component: () =>
                    import ('../views/Coach/CoachCourse'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/groupCourse',
                name: 'GroupCourse',
                component: () =>
                    import ('../views/GroupCourse/GroupCourse'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/banner',
                name: 'Banner',
                component: () =>
                    import ('../views/Banner/Banner'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/integral',
                name: 'Integral',
                component: () =>
                    import ('../views/Integral/Integral'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/syslog',
                name: 'SysLog',
                component: () =>
                    import ('../views/Setting/SysLog'),
                meta: {
                    requireAuth: true
                }
            },
            {
                path: '/loginlog',
                name: 'LoginLog',
                component: () =>
                    import ('../views/Setting/LoginLog'),
                meta: {
                    requireAuth: true
                }
            }, {
                path: '/purpose',
                name: 'Purpose',
                component: () =>
                    import ('../views/Other/Purpose'),
                meta: {
                    requireAuth: true
                }
            }, {
                path: '/feedback',
                name: 'Feedback',
                component: () =>
                    import ('../views/Other/Feedback'),
                meta: {
                    requireAuth: true
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    }
];

const router = new VueRouter({
    routes
});

// //所有页面添加参数
// const query = {
// 	orgCode: store.state.gOrgCode ? store.state.gOrgCode : ''
// }
// // 修改底层方法
// const transitionTo = router.history.transitionTo // 临时存储底层方法
// router.history.transitionTo = function (location, onComplete, onAbort) {
// 	location = typeof location === 'object' ? { ...location, query: { ...location.query, ...query } } : { path: location, query }
// 	transitionTo.call(router.history, location, onComplete, onAbort)
// }

//页面打开之前
router.beforeEach((to, from, next) => {
    // if (to.meta.requireAuth) {
    // 	if (store.state.gToken) {
    // 		//验证是否有权限
    // 		if (to.meta.roles) {
    // 			if (to.meta.roles.includes(store.state.gRoleName)) {
    // 				next()
    // 			} else {
    // 				Message({
    // 					message: '权限不足',
    // 					type: 'error',
    // 					showClose: true
    // 				});
    // 				return Promise.reject(new Error(Message))
    // 			}
    // 		}
    // 		next()
    // 	} else {
    // 		next({
    // 			path:'/login',
    // 			query:{
    // 				orgCode:store.state.gOrgCode ? store.state.gOrgCode : ''
    // 			}
    // 		})
    // 	}
    // } else {
    // 	if (to.path === '/login' && store.state.gToken) {
    // 		next(from.path)
    // 	} else {
    // 		next()
    // 	}
    // }

    next()
});

//router path重复异常处理
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

export default router;